package io.bsly.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.R;
import io.bsly.modules.app.entity.localEntity.DyUserLongTermEntity;
import io.bsly.modules.app.service.localmaster.DyUserLongTermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.parser.TagElement;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;


/**
 * 常住人
 * @author fys
 */
@Api(tags = "常住人管理")
@RestController
@RequestMapping("app/dyuserlongterm")
public class DyUserLongTermController {
    @Autowired
    private DyUserLongTermService dyUserLongTermService;

    /**
     * 列表
     */
    @ApiOperation("分页展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页数" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "int", paramType = "query")
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dyUserLongTermService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		DyUserLongTermEntity dyUserLongTerm = dyUserLongTermService.getById(id);
        return R.ok().put("dyUserLongTerm", dyUserLongTerm);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    public R save(@RequestBody DyUserLongTermEntity dyUserLongTerm){
        DyUserLongTermEntity one = dyUserLongTermService.getOne(new LambdaQueryWrapper<DyUserLongTermEntity>()
                .eq(DyUserLongTermEntity::getUserId, dyUserLongTerm.getUserId())
                .eq(DyUserLongTermEntity::getMobile, dyUserLongTerm.getMobile())
        );
        if (Objects.isNull(one)){
            dyUserLongTermService.save(dyUserLongTerm);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("更新")
    @PostMapping("/update")
    public R update(@RequestBody DyUserLongTermEntity dyUserLongTerm){
		dyUserLongTermService.updateById(dyUserLongTerm);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("根据ID数组删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		dyUserLongTermService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
